﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpUiViewEventHandler.h"
#include "GbmpUiViewMouseEventHandler.h"
#include "IUiViewEventArgs.h"
#include "IUiView.h"
#include "IUiViewMouseEvent.h"
#include "IModelView.h"
#include "ISelection.h"
#include "IHighlights.h"
#include "IPickCandidates.h"
#include "IUiDocument.h"
#include "IDrawingLayoutManager.h"
#include "IUserTransaction.h"
#include "IDrawingViewport.h"
#include "IDrawingLayout.h"
#include "IRegenerator.h"
#include "IPreHighlights.h"
#include "IPreHighlightsChangeEvent.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;


// TODO:临时处理，GBMP视图类型应该从gcmp移除到GBMP来
#define  View3DViewTypePerspective    L"View3DViewTypePerspective"  //  L"三维透视视图"

void GbmpUiViewEventHandler::On(IUiViewEventArgs* pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs, L"pArgs为空",L"GDMPLab",L"2024-03-30");

    if (pArgs->GetType() == UiViewEventType::UiViewActivated)
    {
        IUiView* pUiView = pArgs->GetUiView();
        if (pUiView)
        {
            // 处理三维透视视图，取消当前选择、高亮、拾取
//            {
//                IModelView* pModelView = pUiView->GetModelView();
//                if (pModelView && pModelView->GetUserDataId() == View3DViewTypePerspective)
//
//                {
//                    ISelection::Get()->Clear(pModelView->GetDocument());
//                    IHighlights::Get()->Clear();
//                    IPickCandidates::Get()->Clear();
//                    IActionManager::Reset();
//                }
//            }
            
            {
                IModelView* pModelView = pUiView->GetModelView();
                if (pModelView && pModelView->GetViewType() == BuiltInViewType::Drawing)
                {
                    IUiDocument* pUiDoc = pUiView->GetUiDocument();
                    DBG_WARN_AND_RETURN_VOID_UNLESS(pUiDoc, L"pUiDoc指针无效",L"GDMPLab",L"2024-03-30");
                    
                    std::vector<IUiView*> uiViews = pUiDoc->GetAllUiViews();
                    std::set<ElementId> modelViewIds;
                    FOR_EACH(uiView, uiViews)
                    {
                        ElementId modelViewId = uiView->GetModelViewId();
                        if (modelViewId.IsValid())
                        {
                            modelViewIds.insert(modelViewId);
                        }
                        
                    }

                    IDocument* pDocument = pUiDoc->GetDbDocument();
                    DBG_WARN_AND_RETURN_VOID_UNLESS(pDocument, L"pDoc指针为空",L"GDMPLab",L"2024-03-30");
                    std::wstring errorMessage;
                    IDrawingLayoutManager* pLayoutsManager = IDrawingLayoutManager::Get(pDocument, &errorMessage);
                    DBG_WARN_AND_RETURN_VOID_UNLESS(pLayoutsManager, L"layoutManager为空",L"GDMPLab",L"2024-03-30");
                    IDrawingLayout* pLayout = pLayoutsManager->GetLayout(pModelView->GetName());
                    DBG_WARN_AND_RETURN_VOID_UNLESS(pLayout, L"pLayout为空,没有找到当前视图图纸",L"GDMPLab",L"2024-03-30");

                    std::vector<IDrawingViewport*> viewports = pLayout->GetAllViewports();
                    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDocument, GBMP_TR(L"更新图纸中的视口尺寸"), true);

                    ElementIdArray viewportsToRegen;
                    FOR_EACH(viewport, viewports)
                    {
                        auto iter = std::find(modelViewIds.begin(), modelViewIds.end(), viewport->GetModelViewId());
                        if (iter != modelViewIds.end())
                        {
                            //  viewport->UpdateSize();
                            viewportsToRegen.push_back(viewport->GetOwnerElement()->GetElementId());
                        }
                    }
                    pDocument->GetRegenerator()->ForceRegeneratingElements(viewportsToRegen);
                    if (opTransaction != nullptr)
                        opTransaction->Commit();
                }
            }

            IUiViewMouseEvent* pViewMouseEvent = pUiView->GetUiViewMouseEvent();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pViewMouseEvent, L"pViewMouseEvent为空",L"GDMPLab",L"2024-03-30");
            m_opUiViewMouseEventHandler = NEW_AS_OWNER_PTR(GbmpUiViewMouseEventHandler);
            pViewMouseEvent->Add(m_opUiViewMouseEventHandler.get());

            IPreHighlights *pPreHighlights = IPreHighlights::Get();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pPreHighlights, L"pPreHighlights为空",L"GDMPLab",L"2024-03-30");
            IPreHighlightsChangeEvent *pPreHighlightsChangeEvent = pPreHighlights->GetPreHighlightsChangeEvent();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pPreHighlightsChangeEvent, L"pPreHighlightsChangeEvent为空",L"GDMPLab",L"2024-03-30");
            m_opPreHighLightsChangeEventHandler = NEW_AS_OWNER_PTR(GbmpPreHighLightsChangeEventHandler);
            pPreHighlightsChangeEvent->Add(m_opPreHighLightsChangeEventHandler.get());
        }
    }
    else if (pArgs->GetType() == UiViewEventType::UiViewDeactivated)
    {
        IUiView *pUiView = pArgs->GetUiView();
        if (pUiView)
        {
            IUiViewMouseEvent* pViewMouseEvent = pUiView->GetUiViewMouseEvent();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pViewMouseEvent, L"pViewMouseEvent为空",L"GDMPLab",L"2024-03-30");
            pViewMouseEvent->Delete(m_opUiViewMouseEventHandler.get());

            IPreHighlights *pPreHighlights = IPreHighlights::Get();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pPreHighlights, L"pPreHighlights为空",L"GDMPLab",L"2024-03-30");
            IPreHighlightsChangeEvent *pPreHighlightsChangeEvent = pPreHighlights->GetPreHighlightsChangeEvent();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pPreHighlightsChangeEvent, L"pPreHighlightsChangeEvent为空",L"GDMPLab",L"2024-03-30");
            pPreHighlightsChangeEvent->Delete(m_opPreHighLightsChangeEventHandler.get());
        }
    }
    else if (pArgs->GetType() == UiViewEventType::UiViewCreated)
    {
    }
    else if (pArgs->GetType() == UiViewEventType::UiViewClosed)
    {
    }
}
